﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;

using SBPweb.Communication.Shared.Library.Contacts;
using SBPweb.Communication.Shared.Library.Conversations;

using SBPweb.Communication.Client.Library.Xmpp.Conversations;

namespace SBPweb.Communication.Client.Controls.Xmpp.Conversations
{

    public partial class ConversationsView : UserControlBase
    {

        #region Private fields

        private Dictionary<ConversationBase, TabItem> tabsByConv = new Dictionary<ConversationBase, TabItem>();

        #endregion

        #region Constructor logic

        public ConversationsView()
        {
            InitializeComponent();

            if (Messenger != null)
            {
                Messenger.Initialized += new EventHandler(Messenger_Initialized);

                Messenger.Connections.Connected += new EventHandler(Connections_Connected);
                Messenger.Conversations.ConversationOpened += new EventHandler<ConversationEventArgs>(Conversations_ConversationOpened);
                Messenger.Conversations.ConversationClosed += new EventHandler<ConversationEventArgs>(Conversations_ConversationClosed);
            }
        }

        #endregion

        #region Methods

        #region Event handlers

        private void Messenger_Initialized(object sender, EventArgs e)
        {
            //Application.Messenger.Conversations.OpenConversation("sbpweb");
        }

        private void Connections_Connected(object sender, EventArgs e)
        {
        }

        private void Conversations_ConversationOpened(object sender, ConversationEventArgs e)
        {
            ConversationView ctrlConversation = new ConversationView(e.Conversation);
            TabItem tabItem = new TabItem();
            tabItem.Width = 400;
            tabItem.VerticalAlignment = VerticalAlignment.Stretch;
            tabItem.VerticalContentAlignment = VerticalAlignment.Stretch;
            tabItem.Content = ctrlConversation;
            tabItem.Header = ctrlConversation.Tag;
            tcConversations.Items.Add(tabItem);
            tabsByConv.Add(e.Conversation, tabItem);
        }

        private void Conversations_ConversationClosed(object sender, ConversationEventArgs e)
        {
            TabItem tabItem = tabsByConv[e.Conversation];
            tabItem.Content = null;
            tcConversations.Items.Remove(tabItem);
        }

        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            (Messenger.Conversations as ConversationManager).FindPublicConferences();
            //Application.Messenger.Contacts.GetContactList();
        }

        #endregion

    }

}